<!--
 * @Author: ZHM zhuimin111@163.com
 * @Date: 2023-04-24 18:04:21
 * @LastEditors: ZHM zhuimin111@163.com
 * @LastEditTime: 2023-12-25 14:35:41
-->
<template>
  <el-drawer v-model="visible"
    v-if="visible"
    append-to-body
    class="main-body-center"
    :with-header="false"
    :show-close="false"
    direction="rtl"
    size="100%">
    <div class="rating-edit-nav-head">
      <div class="head-rating-content">
        <div class="head-proj-dropdown">
          <span>
            {{ proName }}
          </span>
          <span class="head-schedule">1/5</span>
        </div>
        <div class="head-time">
          距离评审结束还有： <exam-timer :timeValue="timeValue" @timeout="doHandler()" />
        </div>
      </div>
      <el-tabs v-model="activeName"
        class="operation-tabs">
        <el-tab-pane label="专家评分"
          name="first">
        </el-tab-pane>
        <el-tab-pane label="项目信息"
          name="second">
        </el-tab-pane>
        <el-tab-pane label="底稿文件"
          name="third">
        </el-tab-pane>
      </el-tabs>
      <div class="head-rating-operation">
        <div class="rating-grade-num">
          <span class="grade-label">分值总分：</span>
          <span class="grade-info">
            <span class="grade-num">{{ totalScore }} </span>分
          </span>
        </div>
        <div class="rating-grade-num">
          <span class="grade-label">评审得分：</span>
          <span class="grade-info">
            <span class="grade-num">{{ ratingGrade }} </span>分
          </span>
        </div>
        <div class="rating-tools">
          <div class="tools-btn">
            <el-button class="btn-item"
              type="primary"
              shape="round"
              @click="previewRatingResult">
              预览评审结果
            </el-button>
            <el-button class="btn-item"
              type="primary"
              shape="round"
              @click="saveExamineExpertScoring">
              评审
            </el-button>
            <el-button type="danger"
              plain
              @click="handleClose">关闭</el-button>
          </div>
        </div>
      </div>
    </div>
    <div v-show="activeName === 'first'"
      class="first-scoring">
      <div class="rating-body">
        <expert-scoring ref="expertScoringForm"
          :reviewTaskType="reviewTaskType"
          :activityTaskId="activityTaskId"
          :taskId="taskId"
          :applyId="applyId"
          :proId="proId"
          :setRatingGrade="setRatingGrade"
          :setTotalScore="setTotalScore">
        </expert-scoring>
      </div>
    </div>
    <div v-show="activeName === 'second'"
      class="project-drawer-body">
      <ProjectPreYhsPanel :proId="proIds"
        :showHeader="false"
        :applyId="applyId"
        :formCode="YHS_PROJ_FORM_CONFIG.EXP.formCode" />
    </div>
    <div v-show="activeName === 'third'"
      class="upload-drawer-body">
      <attachment-panel busiType="ET001"
        formType="yhs"
        :busiId='proId'></attachment-panel>
    </div>
    <!-- 填写评审意见 -->
    <examine-ideas-modal ref="examineIdeasForm"
      :submitScoreItems="submitScoreList"
      :applyId="applyId"
      :taskId="taskId"
      :activityTaskId="activityTaskId"
      :reviewTaskType="reviewTaskType"
      :operatorType="operatorType"
      v-model:visible="examineIdeaVisible"
      @closeAll="handleExamineIdeaOk">
    </examine-ideas-modal>
  </el-drawer>
</template>
<script  setup name="OperationLog">
import { RkConfirmBox } from '@/components/message-box/index.js'
import { defineAsyncComponent } from 'vue';
import { ElMessage, ElLoading } from 'element-plus'
//底稿文件
const AttachmentPanel = defineAsyncComponent(() => import('@/components/designer/src/slots/components/AttachmentPanel.vue'));
// 专家评分
import ExpertScoring from "./ExpertScoring.vue"
// 评分项
import ExamineIdeasModal from "./ExamineIdeasModal.vue"
const ProjectPreYhsPanel = defineAsyncComponent(() => import('@/components/preview/src/components/ProjectPreYhsPanel.vue'));
import { YHS_PROJ_FORM_CONFIG } from '@/components/designer/cfg/rk-form-conf.js';
import { previewFile, fileType, openNewWindow } from '@/utils/file-utils.js';
import { useOnlineApi } from '@/api/index';
import ExamTimer from '../components/ExamTimer.vue'
let timeValue = ref(60)

const { onlineReviewResult } = useOnlineApi();
const emits = defineEmits([
  'refresh'
])

const activeName = ref('first')
const visible = ref(false)
const props = defineProps({
  operatorType: {
    type: [Number, String],
    default: ''
  },
  proIds: {
    type: [Number, String],
    default: ''
  },
  projectItem: {
    type: Object,
    default: () => ({})
  }
})

// 评审得分
const ratingGrade = ref(0)
// 总评分
const totalScore = ref(0)
//评分页面
const expertScoringForm = ref(null);

// 打开Drawer
let projectItem = null
let proName = ref('');
let taskId = ref('');
let applyId = ref('');
let activityTaskId = ref(0);
let reviewTaskType = ref(0);
let proId = ref('');

const handleOpen = () => {
  visible.value = true;
}

watch(() => visible, (newValue) => {
  if (visible.value) {
    projectItem = props.projectItem.value
    proName.value = projectItem.proName
    taskId.value = projectItem.reviewTaskId
    applyId.value = projectItem.applyId
    activityTaskId.value = projectItem.taskId
    reviewTaskType.value = projectItem.reviewTaskType
    proId.value = projectItem.proId
    nextTick(() => {
      expertScoringForm.value.getRatingItemList();
      if (reviewTaskType.value == 2) {
        expertScoringForm.value.getOnlineUploadList()
      }
    })

  }
}, { deep: true, immediate: true })

// 关闭Drawer
const handleClose = () => {
  expertScoringForm.value.resertData()
  activeName.value = 'first'
  visible.value = false;
}
const examineIdeaVisible = ref(false)
/** 评审项目内容保存 */
const submitScoreList = ref([]);
const examineIdeasForm = ref(null)
const saveExamineExpertScoring = async () => {
  let validForm = await expertScoringForm.value.submitScoringItemsForm()
  const { valid } = validForm
  if (valid) {
    const { scoreList, dynamicForm } = validForm
    examineIdeasForm.value.setFormData(dynamicForm)
    submitScoreList.value = scoreList
    examineIdeaVisible.value = true
  } else {
    activeName.value = 'first'
  }
}
/** 填写评审意见modal ***************** 开始 */
// 保存评审项目回调
const handleExamineIdeaOk = () => {
  handleClose();
  emits('refresh')
}
//获取评审结果
let previewData = {}
//预览评审结果
const previewRatingResult = async () => {
  const loading = ElLoading.service({
    lock: true,
    text: '生成中...',
  })
  previewData = expertScoringForm.value.getPreviewScoingItemsData();
  const result = await onlineReviewResult(previewData);
  const code = result?.code
  const msg = result?.message
  if (code) {
    ElMessage({ message: msg ?? '业务失败！', type: "error" });
    loading.close()
  } else {
    if (result) {
      let url = null
      loading.close()
      url = previewFile(result, fileType.pdf)
      openNewWindow(url, '预览评审信息')
    } else {
      loading.close()
      ElMessage({ message: '没有任何数据！', type: "warning" });
    }
    loading.close()
  }
}
/** 设置总分 */
const setRatingGrade = (Score) => {
  ratingGrade.value = Score
}
const setTotalScore = (Score) => {
  totalScore.value = Score
}
//自动交卷弹窗
const doHandler= ()=>{
  RkConfirmBox({
			title: '提交评审',
			content: `评审结束，是否进入下一个项目评审？`,
			onSubmit: function () {
			 
			},
			onCancel: function () {},
		});
}

// 对外暴露方法
defineExpose({
  handleOpen,
  handleClose
})

</script>
<style lang="scss" scoped>
:deep(.el-tabs) {
	.el-tabs__nav-wrap::after {
		bottom: -2px;
	}
}

.rating-edit-nav-head {
	display: flex;
	align-items: center;
	justify-content: center;

  position: relative;
	background: hsla(0, 0%, 99.2%, 0.97);
	padding: 0 15px;
	height: 50px;
	line-height: 50px;
   .head-time{
    margin-left: 30px;
    span{
      color: #d65757;
      font-weight: bold;
    }
   }
	.head-rating-content {
    position: absolute;
		display: flex;
		left: 20px;
		.head-title {
			margin-right: 20px;
		}
		.ant-dropdown-link .anticon {
			margin-left: 4px;
		}
    .head-schedule{
      margin-left: 10px;
      color: #d65757;
    }
	}
	.head-rating-operation {
    position: absolute;
		right: 20px;
		display: flex;
		align-items: center;
		.rating-grade-num {
			margin-right: 20px;
			.grade-num {
				color: #d65757;
				margin-right: 5px;
			}
		}
		.rating-tools {
			display: flex;
			.tools-btn {
				.btn-item {
					&:last-child {
						margin-right: 0;
					}
				}
			}
		}
	}
}
.first-scoring {
	height: calc(100% - 50px);
	padding: 10px;
	background: #f0f2f5;
	.rating-body {
		padding: 10px;
		background: #fff;
		height: 100%;
		.link-text {
			.link-icon {
				font-size: 16px;
			}
		}
	}
}

// 中心区域样式
.main-body-center {
	background: #f0f2f5;
	.operation-Log-container {
		height: calc(100% - 50px);
		overflow: auto;
	}
	.project-drawer-body {
		height: calc(100% - 50px);
		.project-preview-yhs-wrapper {
			height: 100%;
		}
	}
	.upload-drawer-body {
		padding: 10px;
		height: calc(100% - 50px);
	}
}
</style>
<style lang="scss">
.main-body-center {
	.el-drawer__body {
		padding: 0;
	}
	// .el-tabs__content {
	//   height: 100%;
	// }
	.el-tabs__header {
		margin: 0;
	}
}
</style>